﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;
using System.Web.Services;
using ProjectWebNhac.DTO;

namespace ProjectWebNhac.GUI.User
{
    public partial class BaiHat : System.Web.UI.Page
    {
        int maBaiHat = 0;
        BaiHatBus bhBus = new BaiHatBus();
        CaSiBus csbus = new CaSiBus();
        AlbumBus abbus = new AlbumBus();
        BinhLuanBaiHatBus blBHBus = new BinhLuanBaiHatBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["MaBH"]!=null)
            {
                maBaiHat = Convert.ToInt16(Request["MaBH"]);
            }
            else
            {
                maBaiHat = 6;
            }

            if (!Page.IsPostBack)
            {
                LoadChiTiet();
                loadPlaylist();
                LoadBinhLuan();
                txtComment.Text = "";
            }
        }
        private void LoadChiTiet()
        {
            DataTable dt = bhBus.User_LayChiTietBaiHat(maBaiHat);
            reDetail.DataSource = dt;
            reDetail.DataBind();

            reThongTinBaiHat.DataSource = dt;
            reThongTinBaiHat.DataBind();

            DataTable dt2 = csbus.LayChiTietCasiTheoBaiHat(maBaiHat);
            reThongTinCaSi.DataSource = dt2;
            reThongTinCaSi.DataBind();

            DataTable dt3 = csbus.LayChiTietCasiTheoBaiHat(maBaiHat);
            int MaCaSi = int.Parse(dt3.Rows[0]["MaCaSi"].ToString());
            reAlbumCungCaSi.DataSource = abbus.LayDanhSachAlbumTheoCaSi(MaCaSi);
            reAlbumCungCaSi.DataBind();

        }

        

        private void loadPlaylist()
        {
            if (Session["ThanhVien"] != null)
            {
                NguoiDungDTO objUser = (NguoiDungDTO)Session["ThanhVien"];
                PlaylistBus plBus = new PlaylistBus();
                dropPlaylist.DataSource = plBus.LayPlaylistTheoTaiKhoan(objUser.TaiKhoan);
                dropPlaylist.DataTextField = "TenPlaylist";
                dropPlaylist.DataValueField = "MaPlaylist";
                dropPlaylist.DataBind();
            }
        }

        protected void reDetail_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            //{
            //    DataTable dt = csbus.LayChiTietCasiTheoBaiHat(maBaiHat);
            //    Image imgHinhAnhCS = (Image)e.Item.FindControl("imgHinhAnhCS");
            //    Label lbTenCS = (Label)e.Item.FindControl("lbTenCS");
            //    Label lbNgaySinh = (Label)e.Item.FindControl("lbNgaySinh");
            //    Label lbCongTy = (Label)e.Item.FindControl("lbCongTy");
            //    Label lbQuocGia = (Label)e.Item.FindControl("lbQuocGia");
            //    Label lbChiTiet = (Label)e.Item.FindControl("lbChiTiet");
            //    imgHinhAnhCS.ImageUrl = "../../HinhAnh/CaSi/" + dt.Rows[0]["HinhAnh"].ToString();
            //    lbCongTy.Text = dt.Rows[0]["CongTyDaiDien"].ToString();
            //    lbNgaySinh.Text = dt.Rows[0]["NgaySinh"].ToString();
            //    lbQuocGia.Text = dt.Rows[0]["TenQuocGia"].ToString();
            //    lbTenCS.Text = dt.Rows[0]["TenCaSi"].ToString();

            //    int MaCaSi = int.Parse(dt.Rows[0]["MaCaSi"].ToString());
            //    Repeater reBaiHatCungCaSi = (Repeater)e.Item.FindControl("reBaiHatCungCaSi");
            //    reBaiHatCungCaSi.DataSource = bhBus.LoadDanhSachBaiHatTheoCaSi(MaCaSi);
            //    reBaiHatCungCaSi.DataBind();

            

            

            //}
        }

        [WebMethod(EnableSession = false)]

        public static bool AddPlayList(string MaPL, string MaBaiHat)
        {
            PlaylistBus plBus = new PlaylistBus();

            return plBus.ThemBaiHatVaoPlayList(Convert.ToInt16(MaPL)
                , Convert.ToInt16(MaBaiHat));

        }
        [WebMethod(EnableSession = false)]

        public static string CreateNewPL(string tenPlaylist, string taikhoan)
        {
            PlaylistBus plBus = new PlaylistBus();
            bool isSuccess = true;
            foreach (DataRow row in plBus.LayPlaylistTheoTaiKhoan(taikhoan).Rows)
            {
                string oldName = row["TenPlaylist"].ToString();
                oldName = oldName.TrimEnd();
                oldName = oldName.ToLower();
                if (oldName.Equals(tenPlaylist.ToLower().TrimEnd()))
                {
                    isSuccess = false;
                }
            }
            if (isSuccess)
            {
                isSuccess = plBus.Taomoiplaylist(taikhoan, tenPlaylist);
            }

            if (isSuccess)
            {
                return plBus.GetMaxIDPlaylist();
            }
            else
            {
                return "failed";
            }
        }

        protected void btnBinhLuan_Click(object sender, EventArgs e)
        {
            BinhLuanBaiHatDTO objBinhLuan = new BinhLuanBaiHatDTO();
           NguoiDungDTO objNguoidung = (NguoiDungDTO)Session["ThanhVien"];
            objBinhLuan.MaBaiHat = maBaiHat;
            objBinhLuan.TaiKhoan = objNguoidung.TaiKhoan;
            objBinhLuan.LoiBinh = txtComment.Text;
            if (blBHBus.ThemBinhLuan(objBinhLuan))
            {
                LoadBinhLuan();
            }
        }

        private void LoadBinhLuan()
        {
            reComment.DataSource = blBHBus.LayListBinhLuan(maBaiHat);
            reComment.DataBind();
        }

        protected void reThongTinBaiHat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater reDsCaSi = (Repeater)e.Item.FindControl("reDsCaSi");
                reDsCaSi.DataSource = csbus.LayCaSiTheoBaiHatTable(maBaiHat);
                reDsCaSi.DataBind();
            }
        }

        protected void reThongTinCaSi_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataTable dt = csbus.LayChiTietCasiTheoBaiHat(maBaiHat);
                int MaCaSi = int.Parse(dt.Rows[0]["MaCaSi"].ToString());
                Repeater reBaiHatCungCaSi = (Repeater)e.Item.FindControl("reBaiHatCungCaSi");
                reBaiHatCungCaSi.DataSource = bhBus.LoadDanhSachBaiHatTheoCaSi(MaCaSi);
                reBaiHatCungCaSi.DataBind();
            }
        }

        protected void reBaiHatCungCaSi_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            int MaBaiHat;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView usrCurrent = e.Item.DataItem as DataRowView;
                MaBaiHat = Convert.ToInt16(usrCurrent["MaBaiHat"]);
                Repeater reDSCaSiBaiHat = (Repeater)e.Item.FindControl("reDSCaSiBaiHat");
                reDSCaSiBaiHat.DataSource = csbus.LayCaSiTheoBaiHatTable(MaBaiHat);
                reDSCaSiBaiHat.DataBind();
            }
        }

        protected void reDetail_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "download")
            {
                string linkbai = e.CommandArgument.ToString();
                Response.ContentType = "audio/mp3";
                Response.AddHeader("content-disposition", "attachment; filename=" + linkbai);
                Response.TransmitFile(Server.MapPath("~/Nhac/" + linkbai));
                Response.End();
            }
        }

        protected void reBaiHatCungCaSi_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "download")
            {
                string linkbai = e.CommandArgument.ToString();
                Response.ContentType = "audio/mp3";
                Response.AddHeader("content-disposition", "attachment; filename=" + linkbai);
                Response.TransmitFile(Server.MapPath("~/Nhac/" + linkbai));
                Response.End();
            }
        }
    }
}